#ifndef CSHAREDDATALINK_H_
#define CSHAREDDATALINK_H_

#include "IBaseDatalink.h"

namespace zl
{
namespace zlbase
{

/// \brief 共享链路
class CSharedDatalink : public IBaseDatalink
{
public:
    CSharedDatalink(std::shared_ptr<IBaseDatalink> datalink) : datalink_(datalink) {}
    ~CSharedDatalink() { datalink_.reset(); }

    inline std::string Origin() override
    {
        return datalink_->Origin();
    }

    inline void AcquireOwnership(bool flag) override
    {
		datalink_->AcquireOwnership(flag);
    }

    inline bool IsOpen() override
    {
        return datalink_->IsOpen();
    }

    inline int32_t Open() override
    {
        if (datalink_->IsOpen())
        {
            return 1;
        }

        return datalink_->Open();
    }

    inline int32_t Close() override
    {
        return 0;
    }

    inline int32_t Read(unsigned char *buff, size_t len) override
	{
		datalink_->SetOption(option_);
        return datalink_->Read(buff, len);
    }

    inline int32_t Write(const unsigned char *buff, size_t len) override
	{
		datalink_->SetOption(option_);
        return datalink_->Write(buff, len);
    }

private:
    std::shared_ptr<IBaseDatalink> datalink_;
};

} // namespace zlbase
} // namespace zl

#endif // CSHAREDDATALINK_H_